<template>
	<div>
		<div class="mine-content" style="padding: 0;margin-top: 0;">
			<div class="company-name1">
				<div class="hasticketMoney">
					<p class="p1">已选择开票金额(元)</p>
					<p class="p2">{{checkMoney?checkMoney.toFixed(2):0}}</p>
				</div>
				<div class="hadticketMoney">
					<div class="ticketLef">
						<p class="p3">{{allMoney?allMoney.toFixed(2):0}}</p>
						<p class="p4">应该开票金额(元)</p>
					</div>
					<div class="ticketRight">
						<p class="p5">{{surplusMoney?surplusMoney.toFixed(2):0}}</p>
						<p class="p6">剩余开票金额(元)</p>
					</div>
				</div>
			</div>
			<div class="tlg">

			</div>
			<ul class="paylist">
				<li v-for="(item,index) in list" :key=index>
					<i class="iconfont " :class="{'icon-shixinduigoudaibiankuang':item.seller.isCheck==true,'icon-kongxinweigou':item.seller.isCheck==false}" @click="chickOne(item,index)"></i>
					<p class="orderNo">合同编号：<span>{{item.orderNo}}</span></p>
					<p class="orderMoney">成交金额：<span>{{item.totalPrice?item.totalPrice:0}}</span></p>
					<p class="discript"><span v-for='(items,index) in item.matetialList' :key=index>{{items}}</span></p>
				</li>
			</ul>
		</div>
		<div class="allchecked">
			<i class="iconfont" :class="{'icon-kongxinweigou':allShow,'icon-shixinduigoudaibiankuang':!allShow}" @click="allCheck"></i>
			<span> 全选</span>
			<button class="btn" @click="confirmClick()">确定 <span class="num"></span></button>
		</div>
	</div>
</template>

<script>
	import {
		getPayList,applyMoney
	} from '../../config/api'
	import {
		getStore
	} from '../../config/mUtils'
	import { Toast } from 'we-vue'
	export default {
		data() {
			return {
				allMoney: 0, //应开票金额
				checkMoney: 0, //已选金额
				surplusMoney: 0, //剩余可选金额
				checklength: 0 ,//可选长度
				list:'',
				allShow:true
			}
		},
		created() {
			//将每个数据通过Object.assign深拷贝一个属性值seller，默认值为false
			getPayList(getStore('SupplierId')).then(res => {
				if(res.code==100){
					res.content.forEach((item,index)=>{
						item.seller = Object.assign({isCheck:false},item.seller)
						this.surplusMoney += item.totalPrice*1
						this.checklength++
						this.allMoney += item.totalPrice*1
					})
					this.list = res.content
				}
			})
		},
		methods: {
			//单选
			chickOne(item,index) {
				if(this.list[index].seller.isCheck){
					this.list[index].seller.isCheck=false
					// this.checklength ++
					this.checkMoney = this.checkMoney - this.list[index].totalPrice*1
				}else{
					// this.checklength -- 
					this.checkMoney = this.checkMoney + this.list[index].totalPrice*1
					this.list[index].seller.isCheck=true
				}
				this.surplusMoney = this.allMoney - this.checkMoney
				if(this.count==this.list.length){
					this.allShow = false
				}else{
					this.allShow = true
				}
			},
			//全选
			allCheck(){
				this.allShow = !this.allShow
				if(this.allShow){
					// this.checklength =0
					for(let i=0;i<this.list.length;i++){
						this.list[i].seller.isCheck = false 
						this.checkMoney -= this.list[i].totalPrice*1
						this.surplusMoney += this.list[i].totalPrice*1					
					}
				}else{
					// this.checklength =2
					this.checkMoney = 0
					for(let i=0;i<this.list.length;i++){
						this.list[i].seller.isCheck = true 
						this.checkMoney += this.list[i].totalPrice*1
						this.surplusMoney = 0
					}
				}
			},
			confirmClick(){
				let id = ''
				this.list.forEach((item,index)=>{
					if(item.seller.isCheck){
						id += item.id + ','
					}
				})
				applyMoney(id).then(res=>{
					if(res.code==100){
						Toast.success(res.content)
						setTimeout(()=>{
							this.$router.push('home')
						},1200)
					}
				})
			}
		}
		
	}
</script>

<style>

</style>
